class ApplicationController < ActionController::Base
	include ExceptionNotifiable rescue nil
	before_filter :authenticate

	def authenticate
		unless session[:user_name]
			begin
				require 'external_auth'
				session[:user_name] = ExternalAuth.get_user(cookies)
			rescue LoadError
			end
		end

		if session[:user_name] 
			@user = User.find_or_create_by_name(session[:user_name])
		end

		if !@user and (WikiOptions[:allow_anonymous_read] == false)
			redirect_to WikiOptions[:access_denied_url]
		end
	end

	def rescue_action_in_public(exception)
		case exception
		when UserError
			@message = exception.message
			render :template => "errors/user"
		when ActiveRecord::RecordNotFound, ::ActionController::UnknownController, ::ActionController::UnknownAction
			render :template => "errors/404"
		else
			render :template => "errors/500"

			deliverer = self.class.exception_data
			data = case deliverer
					 when nil then {}
					 when Symbol then send(deliverer)
					 when Proc then deliverer.call(self)
					 end
			ExceptionNotifier.deliver_exception_notification(exception, self, request, data)
		end
	end
end

class UserError < StandardError
end
